package com.emeraude;

import java.awt.Graphics;
import java.awt.Rectangle;

public abstract class Entity
{

  protected double        x;
  protected double        y;
  protected Sprite        sprite;
  protected double        dx;
  protected double        dy;
  private final Rectangle me  = new Rectangle();
  private final Rectangle him = new Rectangle();

  public Entity(final String ref, final int x, final int y)
  {
    this.sprite = SpriteStore.get().getSprite(ref);
    this.x = x;
    this.y = y;
  }

  public Sprite getSprite()
  {
    return sprite;
  }

  public void setSprite(final Sprite sprite)
  {
    this.sprite = sprite;
  }

  public void move()
  {
    // update the location of the entity based on move speeds
    x += (100 * dx) / 1000;
    y += (100 * dy) / 1000;
  }

  public void setHorizontalMovement(final double dx)
  {
    this.dx = dx;
  }

  public void setVerticalMovement(final double dy)
  {
    this.dy = dy;
  }

  public double getHorizontalMovement()
  {
    return dx;
  }

  public double getVerticalMovement()
  {
    return dy;
  }

  public void draw(final Graphics g)
  {
    sprite.draw(g, (int)x, (int)y);
  }

  public int getX()
  {
    return (int)x;
  }

  public int getY()
  {
    return (int)y;
  }

  /**
   * Check if this entity collised with another.
   * @param other
   *          The other entity to check collision against
   * @return True if the entities collide with each other
   */
  public boolean collidesWith(final Entity other)
  {
    me.setBounds((int)x, (int)y, sprite.getWidth(), sprite.getHeight());
    him.setBounds((int)other.x, (int)other.y, other.sprite.getWidth(), other.sprite.getHeight());

    return me.intersects(him);
  }

  /**
   * Check if this entity collised with another.
   * @param other
   *          The other entity to check collision against
   * @return True if the entities collide with each other
   */
  public boolean CanCollidesWith(final Entity other)
  {
    final double tmpx = x + (100 * dx) / 1000;
    final double tmpy = y + (100 * dy) / 1000;
    me.setBounds((int)tmpx, (int)tmpy, sprite.getWidth(), sprite.getHeight());
    him.setBounds((int)other.x, (int)other.y, other.sprite.getWidth(), other.sprite.getHeight());

    return me.intersects(him);
  }
}
